﻿using DevExpress.Charts.Native;
using DevExpress.Utils;
using DevExpress.Xpo.DB.Helpers;
using DevExpress.XtraCharts.Design;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraPrinting.HtmlExport.Native;
//using DevExpress.XtraReports.Interactivity;
using DevExpress.XtraTreeList;
using Shopfloormanagement;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.ImageList;

namespace Shopfloormanagement
{
    public partial class Step1 : DevExpress.XtraEditors.XtraForm
    {
        public static Step1 instance = new Step1();

        public Step1()
        {
            instance = this;
            InitializeComponent();
        }
        int step1;
        int step2;
        int step3;
        int step4;
        int step5;
        int step6;
        int step7;
        int step8;
        int step9;
        private void LoadA3step1()
        {
            //获取选中A3的数据(团队、计划完成时间)
            DataTable A3Step1 = LSQL.SQL.ReturnDataTable2(SystemInfo.SQL, "A3_Step1", $"A3ReportNumberID = '{rootcause.A3number}'");

            if (A3Step1.Rows.Count > 0)
            {
                txtmember1.Text = A3Step1.Rows[0][2].ToString();
                txtmember2.Text = A3Step1.Rows[0][3].ToString();
                txtmember3.Text = A3Step1.Rows[0][4].ToString();
                txtmember4.Text = A3Step1.Rows[0][5].ToString();
                txtmember5.Text = A3Step1.Rows[0][6].ToString();
                txtmember6.Text = A3Step1.Rows[0][7].ToString();
                txtmember7.Text = A3Step1.Rows[0][8].ToString();
                txtmember8.Text = A3Step1.Rows[0][9].ToString();
                txtmember9.Text = A3Step1.Rows[0][10].ToString();
                txtmember10.Text = A3Step1.Rows[0][11].ToString();

                // 检查是否为空值
                if (A3Step1.Rows[0][12] != null && A3Step1.Rows[0][12] != DBNull.Value && !string.IsNullOrEmpty(A3Step1.Rows[0][12].ToString()))
                {
                    duedate.EditValue = Convert.ToDateTime(A3Step1.Rows[0][12]);
                }
                else
                {
                    duedate.EditValue = null; // 或者设置一个默认值
                }
            }
            else
            {
                return;

            }
            //超期图片变红
            object dbTimeObj = A3Step1.Rows[0]["Duedate"];//获取数据库中的日期数据
            DateTime dbTime;
            if (dbTimeObj != DBNull.Value && DateTime.TryParse(dbTimeObj.ToString(), out dbTime))
            {
                if (dbTime < DateTime.Now)
                {
                    bool reslist = LSQL.SQL.Update(SystemInfo.SQL, "A3_List", "Step1 = 3", $"ID = '{rootcause.A3number}' and Step1 != 1");
                    //Load A3list 获得step1的值
                    DataTable A3listTemp = LSQL.SQL.ReturnDataTable2(SystemInfo.SQL, "A3_List", $"ID = '{rootcause.A3number}'");
                    //获取上方图片状态
                    step1 = (int)A3listTemp.Rows[0][6];
                    step2 = (int)A3listTemp.Rows[0][7];
                    step3 = (int)A3listTemp.Rows[0][8];
                    step4 = (int)A3listTemp.Rows[0][9];
                    step5 = (int)A3listTemp.Rows[0][10];
                    step6 = (int)A3listTemp.Rows[0][11];
                    step7 = (int)A3listTemp.Rows[0][12];
                    step8 = (int)A3listTemp.Rows[0][13];
                    step9 = (int)A3listTemp.Rows[0][14];
                    LoadPicture(step1, step2, step3, step4, step5, step6, step7, step8, step9);
                }
                else
                {
                    return;
                }

            }
        }
        private void LoadA3list()
        {
            //获取选中A3的数据(A3编号，A3描述、责任人)
            DataTable A3list = LSQL.SQL.ReturnDataTable2(SystemInfo.SQL, "A3_List", $"ID = '{rootcause.A3number}'");
            txtA3Reoprtnumber.Text = A3list.Rows[0][3].ToString();
            txtA3ProblemStatement.Text = A3list.Rows[0][4].ToString();
            txtresponsible.Text = A3list.Rows[0][5].ToString();

            //获取上方图片状态
            step1 = (int)A3list.Rows[0][6];
            step2 = (int)A3list.Rows[0][7];
            step3 = (int)A3list.Rows[0][8];
            step4 = (int)A3list.Rows[0][9];
            step5 = (int)A3list.Rows[0][10];
            step6 = (int)A3list.Rows[0][11];
            step7 = (int)A3list.Rows[0][12];
            step8 = (int)A3list.Rows[0][13];
            step9 = (int)A3list.Rows[0][14];
            LoadPicture(step1, step2, step3, step4, step5, step6, step7, step8, step9);
            
        }
        private void Word()//加载进度下方文字
        {
            lblStep1.Text = "Problem\nStatement";
            lblStep2.Text = "Problem\nDescription";
            lblStep3.Text = "Interim\nContainment\nactions";
            lblStep4.Text = "Identify\nPossible\nCauses";
            lblStep5.Text = "Test all\nPossible\nCauses";
            lblStep6.Text = "Verify\nPotential\nRoot Cause";
            lblStep7.Text = "Confirmed\nRoot Cause";
            lblStep8.Text = "Permanent\nCorrective\nActions";
            lblStep9.Text = "Further\nActivities";
            palstep1.BackColor = Color.FromArgb(146, 208, 80);


        }
        private void SetButtonEnable(Control control)//检索所有TextEdit控件是否有变化，如有变化则不显示Next
        {

            if (control is TextEdit)
            {
                control.TextChanged += TextEditTextChange;
            }
            else
            {
                var controlCollection = control.Controls;
                foreach (Control subControl in controlCollection)
                {
                    SetButtonEnable(subControl);
                }
            }
        }
        private void Step1_Load(object sender, EventArgs e)
        {
            //进度进度下方文字加载
            Word();
            // 默认时间为当前日期
            duedate.DateTime = DateTime.Now;
            //获取选中A3的数据(A3编号，A3描述、责任人、进程图片数据)
            LoadA3list();
            //获取选中A3的数据(团队、计划完成时间)
            LoadA3step1();
            SetButtonEnable(this);
            //将默认时间设置为空
            duedate.Properties.NullDate = DateTime.MinValue; // 允许 NULL
            duedate.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
        }
        //加载状态图片
        private void LoadPicture(int condition1, int condition2, int condition3, int condition4, int condition5, int condition6, int condition7, int condition8, int condition9)
        {
            switch (condition1)
            {
                case 1:
                    picstep1.Image = Properties.Resources.G1;
                    break;
                case 2:
                    picstep1.Image = Properties.Resources.B1;
                    break;
                case 3:
                    picstep1.Image = Properties.Resources.R1;
                    break;
                case 4:
                    picstep1.Image = Properties.Resources.H1;
                    break;
                default:
                    picstep1.Image = null;
                    break;
            }
            switch (condition2)
            {
                case 1:
                    picstep2.Image = Properties.Resources.G2;
                    break;
                case 2:
                    picstep2.Image = Properties.Resources.B2;
                    break;
                case 3:
                    picstep2.Image = Properties.Resources.R2;
                    break;
                case 4:
                    picstep2.Image = Properties.Resources.H2;
                    break;
                default:
                    picstep2.Image = null;
                    break;
            }
            switch (condition3)
            {
                case 1:
                    picstep3.Image = Properties.Resources.G3;
                    break;
                case 2:
                    picstep3.Image = Properties.Resources.B3;
                    break;
                case 3:
                    picstep3.Image = Properties.Resources.R3;
                    break;
                case 4:
                    picstep3.Image = Properties.Resources.H3;
                    break;
                default:
                    picstep3.Image = null;
                    break;
            }
            switch (condition4)
            {
                case 1:
                    picstep4.Image = Properties.Resources.G4;
                    break;
                case 2:
                    picstep4.Image = Properties.Resources.B4;
                    break;
                case 3:
                    picstep4.Image = Properties.Resources.R4;
                    break;
                case 4:
                    picstep4.Image = Properties.Resources.H4;
                    break;
                default:
                    picstep4.Image = null;
                    break;
            }
            switch (condition5)
            {
                case 1:
                    picstep5.Image = Properties.Resources.G5;
                    break;
                case 2:
                    picstep5.Image = Properties.Resources.B5;
                    break;
                case 3:
                    picstep5.Image = Properties.Resources.R5;
                    break;
                case 4:
                    picstep5.Image = Properties.Resources.H5;
                    break;
                default:
                    picstep5.Image = null;
                    break;
            }
            switch (condition6)
            {
                case 1:
                    picstep6.Image = Properties.Resources.G6;
                    break;
                case 2:
                    picstep6.Image = Properties.Resources.B6;
                    break;
                case 3:
                    picstep6.Image = Properties.Resources.R6;
                    break;
                case 4:
                    picstep6.Image = Properties.Resources.H6;
                    break;
                default:
                    picstep6.Image = null;
                    break;
            }
            switch (condition7)
            {
                case 1:
                    picstep7.Image = Properties.Resources.G7;
                    break;
                case 2:
                    picstep7.Image = Properties.Resources.B7;
                    break;
                case 3:
                    picstep7.Image = Properties.Resources.R7;
                    break;
                case 4:
                    picstep7.Image = Properties.Resources.H7;
                    break;
                default:
                    picstep7.Image = null;
                    break;
            }
            switch (condition8)
            {
                case 1:
                    picstep8.Image = Properties.Resources.G8;
                    break;
                case 2:
                    picstep8.Image = Properties.Resources.B8;
                    break;
                case 3:
                    picstep8.Image = Properties.Resources.R8;
                    break;
                case 4:
                    picstep8.Image = Properties.Resources.H8;
                    break;
                default:
                    picstep8.Image = null;
                    break;
            }
            switch (condition9)
            {
                case 1:
                    picstep9.Image = Properties.Resources.G9;
                    break;
                case 2:
                    picstep9.Image = Properties.Resources.B9;
                    break;
                case 3:
                    picstep9.Image = Properties.Resources.R9;
                    break;
                case 4:
                    picstep9.Image = Properties.Resources.H9;
                    break;
                default:
                    picstep9.Image = null;
                    break;
            }

        }
        private void TextEditTextChange(object sender, EventArgs e)
        {
            
            btnNext.Visible = false;

        }
        private void btnNext_Click(object sender, EventArgs e)
        {
            if (txtA3ProblemStatement.Text == "")
            {
                MessageBox.Show("项目描述不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;

            }
            else if (txtresponsible.Text == "")
            {
                MessageBox.Show("责任人不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            else if (txtmember1.Text == "" || txtmember2.Text == "")
            {
                MessageBox.Show("至少填写两个团队成员", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            else if (duedate.Text == "")
            {
                MessageBox.Show("预期完成时间不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            else
            {
                
                bool reslist = LSQL.SQL.Update(SystemInfo.SQL, "A3_List", "Step1 = 1", $"ID = '{rootcause.A3number}'");

                if(reslist)
                {
                    
                    Step2 frm = new Step2();
                    frm.Show();
                    this.Hide();
                }
               
            }
        }
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            DataTable A3Step1 = LSQL.SQL.ReturnDataTable2(SystemInfo.SQL, "A3_Step1", $"A3ReportNumberID = '{rootcause.A3number}'");
            if (A3Step1.Rows.Count > 0)
            {
                string contentstep1 = $"Member1 = '{txtmember1.Text}',Member2 = '{txtmember2.Text}',Member3 = '{txtmember3.Text}',Member4 = '{txtmember4.Text}',Member5 = '{txtmember5.Text}', Member6 = '{txtmember6.Text}',Member7 = '{txtmember7.Text}',Member8 = '{txtmember8.Text}',Member9 = '{txtmember9.Text}',Member10 = '{txtmember10.Text}',Duedate = '{duedate.DateTime}'";
                bool resstep1 = LSQL.SQL.Update(SystemInfo.SQL, "A3_Step1", contentstep1, $"A3ReportNumberID = '{rootcause.A3number}'");
                string contentlist = $"A3ProblemStatement = '{txtA3ProblemStatement.Text}',Responsible = '{txtresponsible.Text}',Step1 = 2";
                bool reslist = LSQL.SQL.Update(SystemInfo.SQL, "A3_List", contentlist, $"ID = '{rootcause.A3number}'");
                if (resstep1 == true && reslist == true)
                {

                    MessageBox.Show("更新成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Step1_Load(sender, e);
                    btnNext.Visible = true;
                }
                else
                {
                    MessageBox.Show("更新失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                }

            }
            else
            {
                string column = "[A3ReportNumberID],[Member1],[Member2],[Member3],[Member4],[Member5],[Member6],[Member7],[Member8],[Member9],[Member10],[Duedate]";
                string content = $"'{rootcause.A3number}','{txtmember1.Text}','{txtmember2.Text}','{txtmember3.Text}','{txtmember4.Text}','{txtmember5.Text}','{txtmember6.Text}','{txtmember7.Text}','{txtmember8.Text}','{txtmember9.Text}','{txtmember10.Text}','{duedate.DateTime}'";
                int res = LSQL.SQL.Insert(SystemInfo.SQL, "A3_Step1", column, content);
                if (res >= 1)
                {
                    string contentlist = $"A3ProblemStatement = '{txtA3ProblemStatement.Text}',Responsible = '{txtresponsible.Text}',Step1 = 2";
                    bool reslist = LSQL.SQL.Update(SystemInfo.SQL, "A3_List", contentlist, $"ID = '{rootcause.A3number}'");
                    if (reslist)
                    {
                        MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Step1_Load(sender, e);
                        btnNext.Visible = true;
                    }
                }
                else
                {
                    MessageBox.Show("保存失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                }
            }
        }
        private void btnClosed_Click(object sender, EventArgs e)
        {
            // 弹出确认对话框，询问是否关闭
            DialogResult result = MessageBox.Show("是否确定关闭？", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (result == DialogResult.Yes)
            {
                // 用户选择“是”，执行关闭操作
                this.Close(); // 关闭当前窗体
            }
            else
            {
                // 用户选择“否”，返回，不做任何操作
                return;
            }
        }
        //点击图片跳转功能
        private void picstep1_Click(object sender, EventArgs e)
        {
            if (step1 == 4)
            {
                return;
            }
            else
            { 
                Step1 frm = new Step1();
                this.Close();
                frm.Show();
            }
        }
        private void picstep2_Click(object sender, EventArgs e)
        {
            if (step2 == 4)
            {
                return;
            }
            else
            {
                Step2 frm = new Step2();
                this.Close();
                frm.Show();
            }
        }
        private void picstep3_Click(object sender, EventArgs e)
        {
            if (step3 == 4)
            {
                return;
            }
            else
            {
                Step3 frm = new Step3();
                this.Close();
                frm.Show();
            }
        }
        private void picstep4_Click(object sender, EventArgs e)
        {
            if (step4 == 4)
            {
                return;
            }
            else
            {
                Step4 frm = new Step4();
                this.Close();
                frm.Show();
            }
        }
        private void picstep5_Click(object sender, EventArgs e)
        {
            if (step5 == 4)
            {
                return;
            }
            else
            {
                Step5 frm = new Step5();
                this.Close();
                frm.Show();
            }
        }
        private void picstep6_Click(object sender, EventArgs e)
        {
            if (step6 == 4)
            {
                return;
            }
            else
            {
                Step6 frm = new Step6();
                this.Close();
                frm.Show();
            }
        }
        private void picstep7_Click(object sender, EventArgs e)
        {
            if (step7 == 4)
            {
                return;
            }
            else
            {
                Step7 frm = new Step7();
                this.Close();
                frm.Show();
            }
        }
        private void picstep8_Click(object sender, EventArgs e)
        {
            if (step8 == 4)
            {
                return;
            }
            else
            {
                Step8 frm = new Step8();
                this.Close();
                frm.Show();
            }
        }
        private void picstep9_Click(object sender, EventArgs e)
        {
            if (step9 == 4)
            {
                return;
            }
            else
            {
                Step9 frm = new Step9();
                this.Close();
                frm.Show();
            }
        }
    }
}